define(function(){
    return{
    // 定义接口函数main, 外部调用mai函数即可
    main:function(dom, keywordsData, strFormatData){
            function builtinFormat(strData, key, labelName){
                if(strData.match(eval('/'+key+'/g'))){
                    var str_pice = strData.split(key);
                    for(var i=0; i<str_pice.length; i++){
                        if(i>0){
                            if(str_pice[i]){
                                if(str_pice[i].match(/^[\w\d]/)){;}
                                else{
                                    if(str_pice[i-1]){
                                        if(str_pice[i-1].match(/[\w\d]$/)){;}else{
                                            str_pice[i-1] = str_pice[i-1] + '<' + labelName + '>';
                                            str_pice[i] =  '</' + labelName + '>' + str_pice[i]; 
                                        }
                                    }else{
                                        str_pice[i-1] = '<' + labelName + '>';
                                        str_pice[i] =  '</' + labelName + '>' + str_pice[i]; 
                                    }
                                } 
                            }else{
                                if(str_pice[i-1]){
                                    if(str_pice[i-1].match(/[\w\d]$/)){;}else{
                                        str_pice[i-1] = str_pice[i-1] + '<' + labelName + '>';
                                        str_pice[i] =  '</' + labelName + '>';
                                    }
                                }else{
                                    str_pice[i-1] = '<' + labelName + '>';
                                    str_pice[i] =  '</' + labelName + '>';
                                }
                            }
                        }
                    }
                    return str_pice.join(key);
                }else{
                    return strData
                }

            }

            function numFormat(strData, labelName){
                var numRe = strData.match(/[0-9]+/g);
                if(numRe){
                    for(var num of numRe){
                        strData = builtinFormat(strData, num, labelName);
                    }
                }
                    
                return strData;

            }
            
            function createStyle(label, color){
                var newStyle = document.createElement("style");
                newStyle.appendChild(document.createTextNode(label+'{color:'+color+";}"));
                document.head.appendChild(newStyle);
            }
            
            function normalStr(strData, strList){
                var result_list = [];
                var todo_list = [strData];
                for(var astr of strList){
                    result_list = [];
                    for(var srcStr of todo_list){
                        if(srcStr.match(eval('/'+ astr + '/g'))){
                            var str_pice = srcStr.split(astr);
                            for(var s of str_pice){
                                result_list.push(s)
                            }
                        }
                        else{
                            result_list.push(srcStr);
                        }
                    }
                    todo_list = result_list.slice(0)
                }
                return result_list;
            }
            
            // string 
            function stringFormat(strLine, strList, strLabel){
                var str_pos = [];
                for(var s of strLine){
                    for(var astr of strList){
                        if(s==astr){
                            str_pos.push(astr);
                        }
                    }
                }
                if(str_pos.length != 0){
                    
                    var str_pice = normalStr(strLine, strList)
                    //
                    var result_str_pice = [];
                    var statu = 'null';
                    var _index = -1;
                    for(var i=0; i< str_pos.length; i++){
                        
                        if(statu == 'null'){
                            result_str_pice.push(str_pice[i]);
                            statu = str_pos[i];
                            _index = i;

                            
                        }else if(str_pos[i] == statu){
                            var _temp = str_pice.slice(_index+1, i+1);
                            if(_temp.length == 1){
                                result_str_pice.push( str_pos[i] + _temp[0] + str_pos[i] );
                            }else{
                                result_str_pice.push( str_pos[i] +_temp.join(str_pos[i-1]) + str_pos[i] );
                            }
                            statu = 'null';
                            _index = i;
                            if(i==str_pos.length-1){
                                result_str_pice.push(str_pice[i+1]);
                            }
                        }
                        
                    }
                    // 删选出不是字符串的元素（index为奇数）
                    for(var strIndex=0; strIndex<result_str_pice.length; strIndex++){
                        
                        if(strIndex%2 == 0){
                            // 4.内建处理
                            
                            for(var keyLabel in keywordsData){
                                var keywordList = keywordsData[keyLabel]['keywords'];
                                var strData = result_str_pice[strIndex];
                                for(var key of keywordList){
                                    strData= builtinFormat(strData, key, keyLabel);
                                    result_str_pice[strIndex] = strData;
                                }
                            }
                            // 5.数字处理
                            var numStr = numberFormatData["keywords"];
                            var numLabel = numberFormatData['labelName'];
                            result_str_pice[strIndex] = numFormat(result_str_pice[strIndex], numLabel);
                        }else{
                            result_str_pice[strIndex] = '<'+strLabel+'>' + result_str_pice[strIndex] + '</'+strLabel+'>';
                        }
                        
                    }
                    return result_str_pice.join('');
                }else{
                    // 4.内建处理
                    for(var keyLabel in keywordsData){
                        var keywordList = keywordsData[keyLabel]['keywords'];
                        for(var key of keywordList){
                            strLine= builtinFormat(strLine, key, keyLabel);
                        }
                    }
                    
                    // 5.数字处理
                    var numStr = numberFormatData["keywords"];
                    var numLabel = numberFormatData['labelName'];
                    strLine = numFormat(strLine, numLabel);
                    return strLine
                    
                }
    
            }
            function getPosStr(strData, strList){
                var result_list = [];
                var todo_list = "null";
                var temp = "null";
                var result_temp = [];
                for(var si=0; si<strList.length; si++){
                    astr = strList[si]
                    if(todo_list=="null"){
                        if(strData.match(eval('/'+ astr + '/g'))){
                            todo_list = strData.split(astr);
                            for(var n=0; n< todo_list.length-1; n++){
                                result_temp.push(astr);
                            }  
                        }
                    }else{
                    
                        for(var i=0; i<todo_list.length;i++){
                            srcStr = todo_list[i];
                            if(srcStr.match(eval('/'+ astr + '/g'))){
                                //
                                var str_pice = srcStr.split(astr);
                                    for(var s of str_pice){
                                        result_list.push(s)
                                    }
                                if(i==todo_list.length-1){
                                    for(var n=0; n< str_pice.length-1; n++){
                                        result_temp.push(astr);
                                    }
                                }
                                else{
                                temp = result_temp.slice(i, result_temp.length);
                                result_temp = result_temp.slice(0, i);
                                for(var n=0; n< str_pice.length-1; n++){
                                    result_temp.push(astr);
                                }
                                for(var b of temp){
                                    result_temp.push(b)
                                }
                                }

                                
                            }else{
                                //
                                result_list.push(srcStr)
                                <!-- result_temp.push(astr) -->
                            }
                        }
                        todo_list = result_list.slice(0)
                    
                    }

                    
                }
                // result_temp.reverse()
                return result_temp;
            }
            function threeStringFormat(strLine, strList, strLabel){

                var str_pos = getPosStr(strLine, strList);
                if(str_pos.length != 0){
                    
                    var str_pice = normalStr(strLine, strList)
                    //
                    
                    var result_str_pice = [];
                    var statu = 'null';
                    var _index = -1;
                    for(var i=0; i< str_pos.length; i++){
                        
                        if(statu == 'null'){
                            result_str_pice.push(str_pice[i]);
                            statu = str_pos[i];
                            _index = i;
                        }else if(str_pos[i] == statu){
                            var _temp = str_pice.slice(_index+1, i+1);
                            if(_temp.length == 1){
                                result_str_pice.push( str_pos[i] + _temp[0] + str_pos[i] );

                            }else{
                                result_str_pice.push( str_pos[i] +_temp.join(str_pos[i-1])  + str_pos[i]);
                            }
                            statu = 'null';
                            _index = i;
                            if(i==str_pos.length-1){
                                result_str_pice.push(str_pice[i+1]);
                            }
                        }
                    }
                    
                    // 不是字符串的元素（index为奇数）
                    for(var strIndex=0; strIndex<result_str_pice.length; strIndex++){
                        
                        if(strIndex%2 == 0){
                                result_str_pice[strIndex] = formatHtml(result_str_pice[strIndex]);
                        }else{
                            result_str_pice[strIndex] = '<'+strLabel+'>' + result_str_pice[strIndex] + '</'+strLabel+'>';
                        }
                        
                    }
                    
                    return result_str_pice.join('');
                }else{
                    return formatHtml(strLine);
                }
    
            }
            
            function formatHtml(html_str){
                
                var line_list = html_str.split("\n");
                
                var noteKey = noteFormatData["keywords"];
                var noteLabel = noteFormatData['labelName'];

                var strList = strFormatData["keywords"];
                var strLabel = strFormatData['labelName'];
                for(var lineIndex=0; lineIndex < line_list.length; lineIndex++){
                    
                    // 单行处理
                    var restr=line_list[lineIndex].match(eval("/"+noteKey+"/g"));
                    
                    if(restr){
                        // 有注释（#）
                        var line_pice = line_list[lineIndex].split(noteKey);
                        
                        line_pice[0] = stringFormat(line_pice[0], strList, strLabel);

                        line_pice[0] = line_pice[0] + '<'+noteLabel+'>';
                        line_pice[line_pice.length-1] = line_pice[line_pice.length-1] + '</'+noteLabel+'>';

                        line_list[lineIndex] = line_pice.join(noteKey);
                        
                    }else{
                        // 没有注释
                        line_list[lineIndex] = stringFormat(line_list[lineIndex], strList, strLabel);
                    }
                    
                }
                
                return line_list.join("\n");
                
            }
            
            
            function pythonFormat(dom, labelName){
                // style
                var noteLabel = noteFormatData['labelName'];
                var noteColor = noteFormatData['color'];
                createStyle(noteLabel, noteColor);
                
                var strLabel = strFormatData['labelName'];
                var strColor = strFormatData['color']; 
                createStyle(strLabel, strColor);
                
                var numColor = numberFormatData['color'];
                var numLabel = numberFormatData['labelName'];
                createStyle(numLabel, numColor);
                
                for(var keyLabel in keywordsData){
                    var keyColor = keywordsData[keyLabel]['color'];
                    createStyle(keyLabel, keyColor);
                }
                
                
                var html_str = dom.innerHTML;
                //
                
                threeList = threeStrData["keywords"]
                html_str = threeStringFormat(html_str, threeList, strLabel)
                
                /*
                if(html_str.match(/"""/g)){
                    // 有三点多行注释处理
                    pice_list = html_str.split('"""');
                    for(var piceIndex=0; piceIndex<pice_list.length; piceIndex++){
                        if( piceIndex%2 == 0){
                            pice_list[piceIndex] = formatHtml(pice_list[piceIndex]);
                        }else{
                            pice_list[piceIndex] = '<'+strLabel+'>' + '"""' + pice_list[piceIndex]+ '"""' + '</'+strLabel+'>';
                        }                   
                    }
                    html_str = pice_list.join('');
                    
                }else{
                    // 无三点多行注释处理
                    html_str = formatHtml(html_str);
                }

                */
                //
                dom.innerHTML = html_str;
            }

             pythonFormat(dom);
        }

        
    }

})